

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.annotation.*;
/**
 * Servlet implementation class Structures
 */
@WebServlet("/Structures")
public class Structures extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Structures() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession ses = request.getSession();
		Integer USER_ID = (Integer) ses.getAttribute("USER_ID");
		Integer P_ADDRESS = (Integer) ses.getAttribute("P_ADDRESS");
		
		if(USER_ID==null||P_ADDRESS==null){
			response.sendRedirect("Overview");
			return;
		}
		Database baza = null;
		ResultSet rs = null;
		ArrayList<Integer> aSTRUCT_ID = new ArrayList<>();
		ArrayList<String> aNAME = new ArrayList<String>();
		ArrayList<Integer> aBASE_COST = new ArrayList<>();
		ArrayList<Integer> aINCOME_BASE = new ArrayList<>();
		
		ArrayList<Integer> STRUCT_ID = new ArrayList<>();
		ArrayList<String> NAME = new ArrayList<String>();
		ArrayList<Double> COST = new ArrayList<>();
		ArrayList<Double> INCOME= new ArrayList<>();
		ArrayList<Integer>	LEVEL = new ArrayList<>();
		try {
			baza = new Database();
			rs = baza.Select("STRUCT_ID,NAME,BASE_COST,INCOME_BASE").From("STRUCTURE_TYPES").GetResult();
			while(rs.next()){
				aSTRUCT_ID.add(rs.getInt("STRUCT_ID"));
				aNAME.add(rs.getString("NAME"));
				aBASE_COST.add(rs.getInt("BASE_COST"));
				aINCOME_BASE.add(rs.getInt("INCOME_BASE"));
			}
			rs.close();
			baza.Close();
			baza = new Database();
			rs = baza.Select("*").From("STRUCT_INFO").Where("ADDRESS=" + P_ADDRESS).GetResult();
			while(rs.next()){
				STRUCT_ID.add(rs.getInt("STRUCT_ID"));
				NAME.add(rs.getString("NAME"));
				COST.add(rs.getDouble("COST"));
				INCOME.add(rs.getDouble("INCOME"));
				LEVEL.add(rs.getInt("S_LEVEL"));
			}
			rs.close();
			baza.Close();
			Iterator<Integer> iter = aSTRUCT_ID.iterator();
			while(iter.hasNext()){
				Integer id = iter.next();
				int index = aSTRUCT_ID.indexOf(id);
				if(!STRUCT_ID.contains(id)){
					STRUCT_ID.add(aSTRUCT_ID.get(index));
					NAME.add(aNAME.get(index));
					COST.add(aBASE_COST.get(index).doubleValue());
					INCOME.add(0.);
					LEVEL.add(0);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		request.setAttribute("STRUCT_ID", STRUCT_ID);
		request.setAttribute("NAME", NAME);
		request.setAttribute("COST", COST);
		request.setAttribute("INCOME", INCOME);
		request.setAttribute("LEVEL", LEVEL);
		request.getRequestDispatcher("Structures.jsp").forward(request, response);
		
		
	}
	
	public static int get_next_level_cost(int Address, int StructureID) throws SQLException{
		Database baza = new Database();
		int cena = 0;
		ResultSet rs = baza.Select("*").From("POSITION_STRUCTURES").Where("ADDRESS = " + Address + " AND STRUCT_ID = " + StructureID).GetResult();
		if(rs.isBeforeFirst()){
			rs.next();
			int base_cost = rs.getInt("BASE_COST");
			double cost_factor = rs.getDouble("COST_FACTOR");
			int level = rs.getInt("S_LEVEL");
			cena = (int) (base_cost * Math.pow(cost_factor, level));
		} else {
			baza.Close();
			baza = new Database();
			rs = baza.Select("*").From("STRUCTURE_TYPES").Where("STRUCT_ID = " + StructureID).GetResult();
			rs.next();
			cena = rs.getInt("BASE_COST");
		}
		rs.close();
		baza.Close();
		return Math.abs(cena);
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession ses = request.getSession();
		Integer USER_ID = (Integer) ses.getAttribute("USER_ID");
		Integer P_ADDRESS = (Integer) ses.getAttribute("P_ADDRESS");
		Integer Resources = (Integer) ses.getAttribute("Resources");
		if(USER_ID==null||P_ADDRESS==null){
			response.sendRedirect("Overview");
			return;
		}
		Database baza = null;
		ResultSet rs = null;
		Integer SID = Integer.parseInt(request.getParameter("SID"));
		
		try {
			Resources = Overview.get_current_resources(P_ADDRESS);
			int cost = get_next_level_cost(P_ADDRESS, SID);
			int resID = Overview.get_resID(P_ADDRESS);
			
			if(cost>Resources){
				response.sendRedirect("Structures");
				return;
			} else {
				Resources -=cost;
				Overview.update_resources(Resources, resID);
				}
			baza = new Database();
			rs = baza.Select("*").From("STRUCTURE").Where("ADDRESS = "+ P_ADDRESS + " AND STRUCT_ID = " + SID).GetResult();
			
			if(rs.isBeforeFirst()){
				rs.next();
				Integer lvl = rs.getInt("S_LEVEL");
				
				baza = new Database();
				baza.Update("STRUCTURE").Set("S_LEVEL = "  + (lvl + 1)).Where("STRUCT_ID = " + SID + " AND ADDRESS = " + P_ADDRESS).execute();
				baza.Close();
				
			} else {
				baza.Close();
				baza = new Database();
				baza.InsertInto("STRUCTURE").Values(SID + ", " + P_ADDRESS + ", " + 1).execute();
				baza.Close();
			}
			ses.setAttribute("Resources", Resources);
			response.sendRedirect("Structures");
			return;
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
